package cn.ekuma.beans;
//引用http://twaver.servasoft.com/?attachment_id=1895

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import javax.swing.DefaultCellEditor;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.text.AbstractDocument;
import javax.swing.text.Document;


public class MoneyEditor extends DefaultCellEditor {

    private static class MyTextField extends JTextField {

        private LinePainter painter = new LinePainter();

        public MyTextField() {
            this.setBorder(null);
            this.setMargin(new Insets(0, 0, 0, 0));
            this.setOpaque(false);
            this.setFont(Test.TABLE_FONT);
            this.setHorizontalAlignment(SwingConstants.RIGHT);
            this.addFocusListener(new FocusAdapter() {

                @Override
                public void focusGained(FocusEvent e) {
                    selectAll();
                }
            });
            Document doc= getDocument();
            IntegerDocumentFilter filterOne = new IntegerDocumentFilter();
            ((AbstractDocument)doc).setDocumentFilter(filterOne);
        }

        @Override
        public Insets getInsets() {
            return new Insets(0, 0, 0, 0);
        }

        @Override
        public void paint(Graphics g) {
            Graphics2D g2d = (Graphics2D) g;
            painter.paint(this, g2d, Color.red);
            super.paint(g);
        }
    }

    public MoneyEditor() {
        super(new MyTextField());
        this.setClickCountToStart(1);
    }

}
